package org.testis.data.parser;

import org.testis.data.TestStepDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;


/**
 * A factory to create parser object according to module type of
 * TestStepDataObject
 * @author nurhat
 */
public class TestStepDopFactory
{

    /**
     * A static method that creates a parser object according to 
     * TestStepDataObject's module type.
     * @param moduleType The module type of TestStepDataObject
     * @return a parser object that is base of TestStepDop
     */
    public static TestStepDop getParser(TestStepDataObject data)
    {

        if (data == null) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               " Data Object must not be null",
                                               data.getMessageId());
            return null;
        }
        if (data.getDataToApply() == null) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               " Data to apply  must not be null",
                                               data.getMessageId());
            return null;
        }
        if (data.getModuleType() == null) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               " Module type must not be null",
                                               data.getMessageId());
            return null;
        }
        try {
            Class cl =
                Class.forName("org.testis.data.parser." +
                              data.getModuleType() + "Dop");
            if (cl == null) {
                return null;
            }
            return (TestStepDop) cl.newInstance();
        } catch (InstantiationException ex) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               data.getModuleType() +
                                               " module gives InstantiationException",
                                               data.getMessageId());
        } catch (IllegalAccessException ex) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               data.getModuleType() +
                                               " module gives IllegalAccessException",
                                               data.getMessageId());


        } catch (ClassNotFoundException ex) {
            LogSenderService.putMessageOnQueue("TestDataObjectParserFactory",
                                               AtofLogLevel.ERROR,
                                               data.getModuleType() +
                                               " Module type is not supported right now",
                                               data.getMessageId());

        }
        return null;
    }
}
